<?php
namespace Views;
/**
 * File holds class for Page object. It has general methods useful for all pages. 
 * Here menus and translations are loaded, messages are stored and general 
 * view is put together.
 *
 * PHP version 5.3.5
 *
 * @category   CM
 * @package    Views
 * @subpackage -
 * @author     markus karileet <markuskarileet@hotmail.com>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @link       -
 */
class Page {
	
	/**
	 * Array of translations
	 * @var array
	 */
	protected $_translation;
	/**
	 * Smarty template engine object
	 * @var \Smarty
	 */
	protected $_smarty;
	/**
	 * Currently logged in user
	 * @var \Entities\Account
	 */
	protected $_loggedInUser;
	/**
	 * Array of company IDs that current user manages. This is for fast access
	 * and check
	 * @var array
	 */
	protected $_manageableCompanies;
	/**
	 * Array of messages to display to user 
	 * array('error|warning|success' => 'message string')
	 * @var array
	 */
	protected $_messages;
	/**
	 * Variable holds page content, this is injected into index.tpl content 
	 * section
	 * @var var
	 */
	protected $_content;
	
	/**
	 * Constructor for standalone paegs. Constructor initializes setUp method 
	 */
	public function __construct() {
	    $this->setUp();
	}
	
	/**
	 * Method sets up basic stuff like translations, logged in user details and
	 * company object (if user has one), vertical menu and footer
	 * @global $_SESSION['L'] Language identifier
	 */
	public function setUp() {
		$account = new \Services\Account();
		try {
		    $this->_loggedInUser = $account->checkToken();
		} catch (\Exception $e) {
		    $this->setMessage(array('error' => 'Internal error! Check back later!'));
		}
		$this->_loadLanguage($_SESSION['L']);
		$this->_loadSmarty();
		$this->_loadFooter();
		if ($this->_loggedInUser) {
			$bServ = new \Services\Business();
			//If user is admin, read all businesses, otherwise select 
			//businesses that are related to current account
			if ($this->_loggedInUser->getType() == \Enum\AccountType::Admin) {
				$business = $bServ->getAll();
			} else {
				$business = $bServ->getBusinessByAccount($this->_loggedInUser);
			}
			//For quick reference, create an array of business ID's that current
			//user has rights to manage.
			if ($business) {
				foreach ($business as $b) {
					$this->_manageableCompanies[] = $b->getId();
				}
			}
			//assign logged in user data to template
			$this->_smarty->assign('loggedUser', $this->_loggedInUser);
			//assign logged in user companies to template
			$this->_smarty->assign('company', $business);
		}
		//assign translation array to template
		$this->_smarty->assign('lang', $this->_translation);
		$this->_loadVerticalMenu();
	}
	
	/**
	 * Method for showing static templates (input file is injected into content
	 * object of the page). Right after that, $this->display() is called.
	 * 
	 * So, instead of calling $content = $smarty->fetch('tpl'); display(), you 
	 * can call displayStatic('tpl')
	 * 
	 * TODO: determine if this doubling is nessessary, maybe display function
	 * should do the same thing and this function is no longed nessessary
	 * @param var $pageTemplate Template page filename
	 */
	public function displayStatic($pageTemplate) {
	    $this->_content = $this->_smarty->fetch($pageTemplate);
	    $this->display();
	}
	
	/**
	 * Method populates vertical menu template variable
	 */
	protected function _loadVerticalMenu() {
		$typeService = new \Services\Type();
		//read business types from database and assign to vertical menu
		try {
		    $items = $typeService->getActiveTypes();
		} catch (\Exception $e) {
		    $items = false;  
		}
		$this->_smarty->assign('items', $items);
		
		//populate vNav object with vertical menu navigation html
		$html = $this->_smarty->fetch('verticalNav.tpl');
		$this->_smarty->assign('vNav', $html);
	}
	
	/**
	 * Method populates footer template variable
	 */
	protected function _loadFooter() {
	    //year for copyright statement
		$this->_smarty->assign('yr', date('Y'));
		
		//populate footer object with footer html
		$html = $this->_smarty->fetch('footer.tpl');
		$this->_smarty->assign('footer', $html);
	}
	
	/**
	 * Method loads translation file into class property
	 * @param var $language Language identifier
	 */
	private function _loadLanguage($language) {
		switch ($language) {
			case \Enum\Language::Eng:
				include 'i18n/en.php';
				break;
			case \Enum\Language::Est:
				include 'i18n/et.php';
				break;
			default:
				include 'i18n/et.php';
				break;
		}
		$this->_translation = $lang;		
	}
	
	/**
	 * Method loads smarty object into class attribute
	 */
	private function _loadSmarty() {
		global $smarty;
		$this->_smarty = $smarty;
	}
	
	/**
	 * Method assigns messages to template and then displays general template
	 * (template 'content' variable is assigned with the content from 
	 * $this->_content and 'messages' variable with content from $this->_messages)
	 */
	public function display() {
	    //assign messages object to page
		$this->_smarty->assign('messages', $this->_messages);
		//assign content object to page
		$this->_smarty->assign('content', $this->_content);
		//display index page
		$this->_smarty->display('index.tpl');
	}
	
	/**
	 * Method allows passing messages to another page views
	 * @param array $message Message to pass
	 */
	public function setMessage($message) {
		$this->_messages = $message;
	}
	
	
	/**
	 * Method redirects user to login page with message, that user needs to login
	 * to view this
	 */
	protected function _toLoginPage() {
		$this->setMessage(array('error' => $this->_translation['loginRequired']));
		$this->displayStatic('signin.tpl');
	}
	
	/**
	 * Method is called when service class throws exception. It redirects user
	 * to welcome page with general message that an internal error has occurred!
	 */
	protected function _pagePanic($msg = false) {
		if (!$msg) {
			$msg = 'Internal error occurred! Check back later!';
		}
	    $this->setMessage(array('error' => $msg));
	    $this->displayStatic('welcome.tpl');
	}
}